/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package restService;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.security.auth.login.LoginException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import net.elbandi.pve2api.Pve2Api;
import net.elbandi.pve2api.data.VmOpenvz;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * REST Web Service
 *
 * @author EleygiTheWane
 */
@Path("startVM")
public class StartVMResource {

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of StartVMResource
     */
    public StartVMResource() {
    }

    /**
     * POST method for creating an instance of VmResource
     * @param content representation for the new resource
     * @return an HTTP response with content of the created resource
     * @throws org.json.JSONException
     */
    @POST
    @Consumes("application/json")
    @Produces("application/json")
    public String postJson(String content) throws JSONException {
        System.out.println("Ce qu'on a recu en string "+ content);
        JSONObject request = new JSONObject(content);
        JSONObject response = new JSONObject();
        boolean isExists = false;
        try {
            response.put("result", "false");
            Pve2Api api = new Pve2Api("192.168.100.55", "root", "pam", "passpass");
            api.login();
            System.out.println("login ok");
            //Teste si le vmid existe
            List<VmOpenvz> existingVM = api.getOpenvzCTs("proxmox");
            for(int i=0; i<existingVM.size(); i++){
                if(existingVM.get(i).getVmid() == request.optInt("vmid"))
                    isExists = true;
            }
            
            if(isExists=true){
                api.startOpenvz("proxmox", request.optInt("vmid"));
                response.put("result", "true");
                
                /*if("running".equals(api.getOpenvzCT("proxmox", request.optInt("vmid")).getStatus())){
                    System.out.println("La vmid "+request.optInt("vmid")+" est déjà en marche");
                }
                else{
                    System.out.println("La vmid "+request.optInt("vmid")+" n'est pas en marche");
                    api.startOpenvz("proxmox", request.optInt("vmid"));
                    response.put("result", "true");
                }*/
            
            }        
            
        } catch (LoginException | IOException ex) {
            return response.toString();
            //Logger.getLogger(StartVMResource.class.getName()).log(Level.SEVERE, null, ex);
            
        }
        
        return response.toString();
    }
    
    /**
     * Retrieves representation of an instance of restService.StartVMResource
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("application/json")
    public String getJson() {
        //TODO return proper representation object
        throw new UnsupportedOperationException();
    }

    /**
     * PUT method for updating or creating an instance of StartVMResource
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/json")
    public void putJson(String content) {
    }
}
